import { login, createGroup } from '../../api/login'

Page({
  data: {},
  handleLogin() {
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        wx.login({
          success: (data) => {
            this.authImport({
              userInfo: res.userInfo,
              code: data.code
            })
          }
        })
      }
    })
  },
  async authImport({userInfo, code}) {
    const data = {...userInfo, code}
    data.nickName = data.nickName.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "")
    const res = await login(data)
    this.createGroup(res.id, userInfo)
  },
  async createGroup(id, userInfo) {
    const data = {...userInfo, id}
    data.nickName = data.nickName.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "")
    const res = await createGroup(data)
    if (res.ActionStatus === 'OK') {
      wx.navigateBack()
    }
  }
})